package com.example.wx.dailyreport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wx.dailyreport.mapper.UsercollectionMapper;
import com.example.wx.dailyreport.mapper.WordsupportMapper;
import com.example.wx.dailyreport.mapper.WxwordMapper;
import com.example.wx.dailyreport.pojo.Usercollection;
import com.example.wx.dailyreport.pojo.Wxword;
import com.example.wx.dailyreport.service.WxwordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hulinbo
 * @since 2020-08-08
 */
@Service
@Transactional
public class WxwordServiceImpl extends ServiceImpl<WxwordMapper, Wxword> implements WxwordService {

    @Autowired
    private WxwordMapper wxwordMapper;

    @Autowired
    private WordsupportMapper wordsupportMapper;

    @Autowired
    private UsercollectionMapper UsercollectionMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public IPage<Wxword> selectWordInPageBy(Wxword wxword, String username, Integer order, int page, int limit) {
        IPage<Wxword> iPage = new Page<>(page, limit);
        QueryWrapper<Wxword> wxwordQueryWrapper = new QueryWrapper<>();
        wxwordQueryWrapper.lambda().like(StringUtils.isNotEmpty(wxword.getWord()), Wxword::getWord, wxword.getWord())
                .like(StringUtils.isNotEmpty(wxword.getWordexplain()), Wxword::getWordexplain, wxword.getWordexplain());
        if (order == null) {
            wxwordQueryWrapper.lambda().orderByAsc(Wxword::getWord).orderByDesc(Wxword::getSupportnum, Wxword::getCollectionnum);
        } else if (order == 1) {
            wxwordQueryWrapper.lambda().orderByDesc(Wxword::getSupportnum).orderByAsc(Wxword::getWord).orderByDesc(Wxword::getCollectionnum);
        } else if (order == 2) {
            wxwordQueryWrapper.lambda().orderByDesc(Wxword::getCollectionnum).orderByAsc(Wxword::getWord).orderByDesc(Wxword::getSupportnum);
        }
        iPage = wxwordMapper.selectPage(iPage, wxwordQueryWrapper);

        List<Wxword> wxwordFlags = new LinkedList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        WordsupportMapper wsMapper = sqlSession.getMapper(WordsupportMapper.class);
        UsercollectionMapper ucMapper = sqlSession.getMapper(UsercollectionMapper.class);
        for (Wxword w : iPage.getRecords()) {
            w.setSupportflag(wsMapper.selectByWordIdAndUsername(username, w.getId()) != null);
            w.setCollectionflag(ucMapper.selectByUsernameAndWordId(username, w.getId()) != null);
            wxwordFlags.add(w);
        }
        try {
        } finally {
            sqlSession.close();
        }
        iPage.setRecords(wxwordFlags);
        return iPage;
    }

    @Override
    public IPage<Wxword> selectMyWord(String username, int page, int limit) {
        IPage<Usercollection> uIPage = new Page<>(page,limit);
        QueryWrapper<Usercollection> uqueryWrapper = new QueryWrapper<>();
        uqueryWrapper.lambda().eq(Usercollection::getUsername, username);
        uIPage = UsercollectionMapper.selectPage(uIPage, uqueryWrapper);
        List<Integer> ids = new ArrayList<>();
        for (Usercollection u : uIPage.getRecords())
            ids.add(u.getWordId());
        List<Wxword> list = wxwordMapper.selectBatchIds(ids);

        SqlSession sqlSession = sqlSessionFactory.openSession();
        WordsupportMapper wsMapper = sqlSession.getMapper(WordsupportMapper.class);
        List<Wxword> wxwords = new ArrayList<>();
        for (Wxword w : list) {
            w.setSupportflag(wsMapper.selectByWordIdAndUsername(username, w.getId()) != null);
            w.setCollectionflag(true);
            wxwords.add(w);
        }
        try {
        } finally {
            sqlSession.close();
        }
        IPage<Wxword> iPage = new Page<>();
        iPage.setRecords(wxwords);
        iPage.setTotal(uIPage.getTotal());
        return iPage;
    }

}
